<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Location extends MX_Controller 
{
	function __construct() 
	{
		parent::__construct();	
		$this->load->config('config_site') ;
		$this->load->model('location_model','location');
		$this->pre_message="";
	}
	
	function index()
	{
		$data['title'] = "Đường đi đến công ty Printing Nano" ;
		$data['page'] = 'location/index' ;
		$this->load->view('layout/skin', $data);
	}
	
	function action()
	{
		$seg=$this->uri->segment(1);
		if(!empty($_POST['btnlocation']))
		{
			$this->form_validation->set_rules('cus_name','Tên','trim|required');
			$this->form_validation->set_rules('cus_address','Địa chỉ','trim|required');
			$this->form_validation->set_rules('cus_mail','Email','trim|required');
			$this->form_validation->set_rules('cus_phone','Phone','trim|required');
			$this->form_validation->set_rules('cus_title','Tiêu đề','trim|required');
			$this->form_validation->set_rules('cus_content','Nội dung','trim|required');
			$captcha = $this->input->post('lb_secury');
		    $security_code=$this->session->userdata('security_code');
			if($this->form_validation->run() == false)
			{
				$this->pre_message = validation_errors();	
			}
			else
			{
				if($captcha!=$security_code){
					$this->session->set_flashdata('message','Mã bảo vệ không hợp lệ.');
					redirect($seg.'/location');
				}
				$req = $this->_buildform();
				if($this->location->save('location',$req))
				{
					$this->session->set_flashdata('message','Lưu thành công');
					redirect($seg.'/location/thanhcong');
				}
				else
				{
					$this->pre_message = 'Có lỗi xãy ra, Vui lòng liên hệ qua điện thoại. Cảm ơn';
					
				}
			}
		}
		redirect($seg.'/location');
	}
	
	function _buildform($flag=1)
	{
		$req['lb_cusname'] = $this->input->post('cus_name');
		$req['lb_address'] = $this->input->post('cus_address');
		$req['lb_cusmail'] = $this->input->post('cus_mail');
		$req['nb_phone'] = $this->input->post('cus_phone');
		$req['lb_link'] = $this->input->post('cus_room');
		$req['lb_subject'] = $this->input->post('cus_title');
		$req['lb_content'] = $this->input->post('cus_content');
		if($flag) $req['dt_create'] = date('Y-m-d H:i:s');
		$req['bl_active'] = 1;
		return $req ;
	}
	
	function thanhcong()
	{
		$data['title'] = "Liên hệ thành công" ;
		//$data['cont'] = "Gởi thông tin thành công" ;
		$data['page'] = 'location/thanhcong' ;
		$this->load->view('layout/skin', $data);
	}
	
	function getcaptcha()
	{
		$md5_hash = md5(rand(0,999)); 
		$security_code = substr($md5_hash, 15, 5); 
		//$_SESSION["security_code"] = $security_code;
		$this->session->set_userdata(array('security_code'=>$security_code)) ;
		$width = 100; 
		$height = 28;  
		$image = imagecreate($width, $height);  
		$white = imagecolorallocate($image, 255, 255, 255); 
		$black = imagecolorallocate($image, 0, 0, 0); 
		imagefill($image, 0, 0, $black); 
		imagestring($image, 10, 28, 6, $security_code, $white); 
		header("Content-Type: image/jpeg"); 
		imagejpeg($image); 
		imagedestroy($image); 
	}
}
?>